<?php
/**
 * 三方优惠券入口
 */

namespace App\Kuafu\Order\Controller\Pos;

use App\Exceptions\BaseException;
use App\Exceptions\CustomValidateException;
use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\CouponExt\Channel\CouponExtBase;
use App\Kuafu\CouponExt\Enum\CouponExtEnum;
use App\Kuafu\CouponExt\Exceptions\CouponExtException;
use App\Kuafu\Order\Service\OrderCouponExternalService;
use App\Kuafu\Order\Validate\CouponExtValidate;
use App\Kuafu\Pay\Service\PayService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class CouponExtController extends BaseController
{
    /**
     * 获取美团优惠券详情接口
     *
     * @return JsonResponse
     */
    public function getMeiTuanCouponInfo(Request $request): JsonResponse
    {
        $couponCode = (string)$request->get('code');

        $model = new OrderCouponExternalService(CouponExtEnum::EXT_PLATFORM_MEITUAN, $this->shopId, $this->brandId);
        $data  = $model->getCouponExtInfo($couponCode, true);

        return $this->success($data);
    }

    /**
     * 获取抖音优惠券信息
     *
     * @param Request $request
     * @return JsonResponse
     * @throws BaseException
     * @throws CouponExtException
     * @throws GuzzleException
     */
    public function getDouYinCouponInfo(Request $request): JsonResponse
    {
        $couponCode = (string)$request->get('code');

        $model = new OrderCouponExternalService(CouponExtEnum::EXT_PLATFORM_DOUYIN, $this->shopId, $this->brandId);
        $data  = $model->getCouponExtInfo($couponCode, true);

        return $this->success($data);
    }


    /**
     * 快速核销所有拳
     *
     * @param Request $request
     * @return JsonResponse
     * @throws BaseException
     * @throws CouponExtException
     * @throws GuzzleException
     */
    public function quickAll(Request $request): JsonResponse
    {
        $couponCode = (string)$request->get('code');
        $couponFrom = (int)$request->get('couponFrom', -1);

        $data = OrderCouponExternalService::quickAllCoupon((int)$this->shopId, $couponCode, $couponFrom);

        return $this->success($data);
    }


    /**
     * 校验优惠券是否可以撤销
     *
     * @return JsonResponse
     * @throws GuzzleException
     */
    public function checkCancelCoupon(Request $request)
    {
        $couponCode = (string)$request->get('code');

        $data = PayService::sCheckCancelCoupon($this->shopId, $couponCode);

        return $this->success([
            'status' => $data
        ]);
    }


    /**
     * [小红书券码] 详情
     *
     * @param Request $request
     * @return JsonResponse
     * @throws CouponExtException
     * @throws GuzzleException
     * @throws CustomValidateException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface|BaseException
     */
    public function getXhsCouponDetail(Request $request): JsonResponse
    {
        $params = CouponExtValidate::getXhsCouponDetailValidate($request->all());
        $model  = new OrderCouponExternalService(CouponExtEnum::EXT_PLATFORM_XHS, $this->shopId, $this->brandId);
        return $this->success($model->getCouponExtInfo($params->code));
    }
}
